package com.source.app.discriminate.train;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class Identy{
	
	private HashMap<Integer, String> labelMap = null;
	
	public Identy(){
		loadLabelMap();
	}
	
	private void loadLabelMap(){
		labelMap = new HashMap<Integer, String>();
		BufferedReader reader = null;
		try {
			 reader = new BufferedReader(new FileReader(new File("/Users/admin/image/svm/label.txt")));
			 String buff = null;
			 while((buff = reader.readLine()) != null){
				 String[] arr = buff.split(" ");
				 labelMap.put(Integer.parseInt(arr[1]), arr[0]);
			 }
			 
			 System.out.println("load image label finish!");
			 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	private String getClassName(int label){
		return labelMap.get(label);
	}
	
	/**
	 * 具体的预测，返回识别的文字
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public String predict(File file) throws IOException{
		BufferedImage sourceImage = ImageIO.read(file);
		Preprocess preprocess = new Preprocess();
		BufferedImage binaryImage = preprocess.getBinaryImage(sourceImage);
		
		SegCfg segCfg = new SegCfg();
		ArrayList<BufferedImage> interList = segCfg.cfs(binaryImage);
		
		ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();
		SegWaterDrop segWaterDrop = new SegWaterDrop();
		for (BufferedImage img : interList) {
			ArrayList<BufferedImage> tmpList = segWaterDrop.drop(img);
			for (BufferedImage sumImg : tmpList) {
				imageList.add(ImageUtil.scaleImage(sumImg));
			}
		}
		
		for (int i = 0; i < imageList.size(); i++) {
			ImageIO.write(imageList.get(i), "JPG", new File("/Users/admin/image/tmp/" + i + ".jpg"));
		}
		
		Predict.run(imageList);
		
		String result = "";
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File("/Users/admin/image/svm/result.txt")));
			String buff = "";
			
			while((buff = reader.readLine()) != null){
				int label = (int)Double.parseDouble(buff);
				String className = getClassName(label);
				result += className + "";
			}
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if (reader != null) {
				reader.close();
			}
		}
		return result;
	}
	
	
	public String testpredict(File file) throws IOException{
		BufferedImage sourceImage = ImageIO.read(file);
		Preprocess preprocess = new Preprocess();
		BufferedImage binaryImage = preprocess.getBinaryImage(sourceImage);
		
		SegCfg segCfg = new SegCfg();
		ArrayList<BufferedImage> interList = segCfg.cfs(binaryImage);
		
		ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();
		SegWaterDrop segWaterDrop = new SegWaterDrop();
		if(interList.size()==4){
			for (int i = 0; i < imageList.size(); i++) {
				ImageIO.write(imageList.get(i), "JPG", new File("/Users/admin/image/tmp/" + i + ".jpg"));
			}
			
		}else if(interList.size()<=4){
			for (BufferedImage img : interList) {
				ArrayList<BufferedImage> tmpList = segWaterDrop.drop(img,(4-(interList.size()-1)));
				for (BufferedImage sumImg : tmpList) {
					imageList.add(ImageUtil.scaleImage(sumImg));
				}
			}
		}
		
		for (int i = 0; i < imageList.size(); i++) {
			ImageIO.write(imageList.get(i), "JPG", new File("/Users/admin/image/tmp/" + i + ".jpg"));
		}
		
		Predict.run(imageList);
		
		String result = "";
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File("/Users/admin/image/svm/result.txt")));
			String buff = "";
			
			while((buff = reader.readLine()) != null){
				int label = (int)Double.parseDouble(buff);
				String className = getClassName(label);
				result += className + "";
			}
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if (reader != null) {
				reader.close();
			}
		}
		return result;
	}
	
	public static void main(String[] args) throws IOException{
		Identy index = new Identy();
//		index.predict(new File("download/1.jpg"));
//		String x = index.testpredict(new File("/Users/admin/images/HWNJ.png"));
		
		File dir = new File("/Users/admin/images_/");
		File[] files = dir.listFiles(new FilenameFilter() {
			public boolean isJpg(String file){   
			    if (file.toLowerCase().endsWith(".png")){   
			      return true;   
			    }else{   
			      return false;   
			    }   
			}
			@Override
			public boolean accept(File dir, String name) {
				return isJpg(name);
			}
		});
		
		for (File file : files) {
			try {
				String x = 	index.testpredict(file);
				System.out.println(file.getName()+"---------"+x);
			} catch (IOException e) {		
				e.printStackTrace();
			}
		}
		
	}
}
